# Copyright (C) 2007 "dbsolution sas" 
#
# This file is part of Weebotee.
#
# Weebotee is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Weebotee is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Weebotee.  If not, see <http://www.gnu.org/licenses/>.


import datetime
from django import newforms as forms
from apps.core.models.element import Element
from apps.basecontent.models.article import Article
from apps.core.backend.views.forms import element_form

class ArticleForm(element_form.ElementForm):
    # Parametri descrittivi
    text = forms.CharField(label='Testo', required=False)
    excerpt = forms.CharField(label='Riassunto', required=False)
    order = forms.CharField(label="Ordinamento")
    id = None
    
    def __init__(self, *args, **kwargs):
        self.base_fields['text'].widget = forms.widgets.Textarea()
        self.base_fields['excerpt'].widget = forms.widgets.Textarea()
        super(ArticleForm, self).__init__(*args, **kwargs)
        try:
            self.id = kwargs['initial']['id']
        except:
            pass

    def save(self, *args, **kwargs):
        if self.id == None:
            a = Article() 
        else:
            a = Article.objects.filter(related_element= self.id)[0]
        a.related_element = super(ArticleForm, self).save(tp="basecontent:Article",*args, **kwargs)    
        a.text = self.cleaned_data['text']
        a.excerpt = self.cleaned_data['excerpt']
        a.order = self.cleaned_data['order']
        
        a.save()
        return
